using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using CE.Global;

public partial class controls_usrNationality : System.Web.UI.UserControl
{
    protected string strNationality = "Select";
    protected string strValidatinoGroup = "";
    protected string strErrorMessage = "Choose nationality.";
    protected bool bEnabled = true;
    bool bReadOnly = false;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            DataTable dtNationality = General.GetComboItem("TH.Nationality", false);
            ddlNationality.DataSource = dtNationality;
            ddlNationality.DataTextField = dtNationality.Columns[0].ColumnName.ToString();
            ddlNationality.DataValueField = dtNationality.Columns[1].ColumnName.ToString();
            ddlNationality.DataBind();
            ddlNationality.Items.Insert(0, new ListItem("Select", "-1"));

            ddlNationality.Attributes.Add("onblur", "ShowValidationEffect('" + ddlNationality.ClientID + "','" + rfvNationality.ClientID + "');");
        }
    }

    public void ChangeValidationEffectServerSide(string strValidationGroup)
    {
        if (rfvNationality.Enabled && rfvNationality.ValidationGroup == strValidationGroup)
        {
            rfvNationality.Validate();
            if (rfvNationality.IsValid == false)
            {
                ddlNationality.ControlStyle.CssClass = "validationError";
            }
            else
            {
                ddlNationality.ControlStyle.CssClass = "";
            }
        }
        else
        {
            ddlNationality.ControlStyle.CssClass = "";
        }
    }

    //protected void ServerValidation(object source, ServerValidateEventArgs args)
    //{
    //    // Test whether the value is -1
    //    if (args.Value != "-1")
    //    {
    //        args.IsValid = true;
    //    }
    //    else
    //    {
    //        args.IsValid = false;
    //    }
    //}

    public bool IsEnabled
    {
        get { return bEnabled; }
        set
        {
            bEnabled = value;
            SetEnabled();
        }
    }

    public bool IsReadOnly
    {
        get { return bReadOnly; }
        set
        {
            bReadOnly = value;
            //SetReadOnly();
        }
    }

    public void IsEnableValidation(bool bValue)
    {
        rfvNationality.Enabled = bValue;
    }

    public string Nationality
    {
        get { return strNationality; }
        set { strNationality = value; }
    }

    public string ValidationGroup
    {
        get { return strValidatinoGroup; }
        set { strValidatinoGroup = value; }
    }

    public string ErrorMessage
    {
        get { return strErrorMessage; }
        set { strErrorMessage = value; }
    }

    private void SetEnabled()
    {
        //ddlNationality.SelectedIndex = 0;
        ddlNationality.Enabled = bEnabled;
        rfvNationality.Enabled = bEnabled;
    }

    protected override void OnPreRender(EventArgs e)
    {
        //SetEnabled();

        if (strNationality != string.Empty)
        {
            if (strNationality != "Select")
            {
                ListItem lstNationality = ddlNationality.Items.FindByText(strNationality);
                if (lstNationality != null)
                {
                    ddlNationality.Text = lstNationality.Value;
                }
            }
        }

        if (strValidatinoGroup != null)
        {
            if (strValidatinoGroup != string.Empty)
                rfvNationality.ValidationGroup = strValidatinoGroup;
        }

        if (bReadOnly)
        {
            SetReadOnly();
        }

        if (strErrorMessage != string.Empty)
        {
            //rfvNationality.ErrorMessage = strErrorMessage;
        }

    }

    private void SetReadOnly()
    {
        if (bReadOnly)
        {
            ddlNationality.Text = strNationality;

            string orgNationalityText = ddlNationality.SelectedItem.Text;
            string orgNationalityValue = ddlNationality.SelectedItem.Value;

            ddlNationality.Items.Clear();

            ddlNationality.Items.Add(new ListItem(orgNationalityText, orgNationalityValue));
        }
    }

    public string GetNationality()
    {
        strNationality = ddlNationality.SelectedItem.Text;
        return strNationality;
    }
}
